package com.jpush.gateway.config;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.server.authentication.ServerAuthenticationConverter;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class WebFluxTokenAuthenticationConverter implements ServerAuthenticationConverter {

    @Override
    public Mono<Authentication> convert(ServerWebExchange exchange) {
        String header = exchange.getRequest().getHeaders().getFirst("Authorization");
        if (header == null) {
            return Mono.error(new RuntimeException("header 中 Authorization 不能为空！"));
        }
        // 从head 获取用户信息
        if (header.startsWith("Bearer ")) {

        }
        UsernamePasswordAuthenticationToken usernameToken = new UsernamePasswordAuthenticationToken(1, null, new ArrayList<>());
        Map<String,Object> jwtPlayload = new HashMap<>();
        jwtPlayload.put("userNo",1);
        jwtPlayload.put("orgNo",1);
        jwtPlayload.put("name","小苏");
        usernameToken.setDetails(jwtPlayload);
        return Mono.just(usernameToken);
    }
}
